<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\CityList;
use App\Models\RankingTips;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class RankingTipsController extends Controller
{
    use HasResourceActions;
    private $name = '活跃度排行提示';

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.list'))
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.detail'))
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.edit'))
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.create'))
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new RankingTips);
//        $grid->model()->where('status', '>', 0)->orderBy('id', 'desc');
//
//        $grid->id('ID')->sortable();
//        $grid->cityInfo()->name('城市');
//        $grid->title('标题');
//        $grid->RankingTips_time('时间');
//        $grid->status('状态')->display(function ($status) {
//            return RankingTips::statusArray($status);
//        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(RankingTips::findOrFail($id));

        $show->id('ID');
        $show->title('标题');
        $show->remark('简介');
        $show->status('状态')->unescape()->as(function ($status) {
            return RankingTips::statusArray($status);
        });
        $show->panel()
            ->tools(function ($tools) {
                $tools->disableList();
                $tools->disableDelete();
            });;
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new RankingTips);


        $form->text('title', '标题')->rules('required');
        $form->textarea('remark', '简介')->rows(10)->rules('required');
        $states = [
            'on' => ['value' => 20, 'text' => '发布', 'color' => 'success'],
            'off' => ['value' => 10, 'text' => '隐藏', 'color' => 'danger'],
        ];
        $form->switch('status', '状态')->states($states)->default(10);

        $form->footer(function ($footer) {
            // 去掉`重置`按钮
            $footer->disableReset();
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
        });
        $form->tools(function (Form\Tools $tools) {
            // 去掉`列表`按钮
            $tools->disableList();
            // 去掉`删除`按钮
            $tools->disableDelete();
            // 去掉`查看`按钮
            $tools->disableView();
        });
        return $form;
    }

    public function confirm($id)
    {
        $RankingTips = RankingTips::find($id);
        if ($RankingTips->status != 10) {
            admin_toastr('当前新闻已经发布了', 'error');
            return back();
        }
        $RankingTips->status = 20;
        if ($RankingTips->save()) {
            admin_toastr('确认成功', 'success');
        } else {
            admin_toastr('确认失败', 'error');
        }

        $url = route('RankingTips.index');
        return redirect($url);
    }
}
